/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint-disable no-use-before-define */
/* eslint-disable import/no-extraneous-dependencies */
// 信息验证组件，未维护公司信息的，将弹窗提醒
import React, { Fragment, useEffect, useCallback } from 'react';
import { Modal } from 'antd';
import router from 'umi/router';
import { connect } from 'dva';

const infoMap = {
  post: '欢迎注册中邮共配管理系统，请先完善公司信息！',
  yz: '欢迎注册快宝智慧快递管理平台，请先完善公司信息！',
  yjy: '欢迎注册驿加易共配管理系统，请先完善公司信息！',
  td: '欢迎注册智慧快递管理平台，请先完善公司信息！',
};

const InfoCheck = props => {
  const {
    children,
    loading,
    options: { key, subKey },
  } = props;
  const isYjy = key === 'yjy';

  const showInfoModal = useCallback(
    ({ onReady, currentUser }) => {
      const { name, phone, user_info = {} } = currentUser;
      const { branchLevel } = user_info;
      if (!name || !phone) {
        // 驿加易区县一级需要提示，其他层级不需要
        if (isYjy && branchLevel != 3) {
          onReady({ ...currentUser, name: 'name' });
          return;
        }
        Modal.info({
          title: '系统提示',
          content: (
            <div>
              <p>{infoMap[subKey || key]}</p>
            </div>
          ),
          okText: '确定',
          centered: true,
          onOk() {
            router.push({
              pathname: '/system/info',
            });
          },
        });
      } else {
        onReady(currentUser);
      }
    },
    [loading, key, isYjy],
  );

  useEffect(
    () => {
      if (loading != 'loading') {
        showInfoModal(props);
      }
    },
    [loading],
  ); // 监听info的status状态获取最新的Props

  return <Fragment>{children}</Fragment>;
};

InfoCheck.defaultProps = {
  onReady: () => {},
};

export default React.memo(
  connect(({ user, info, setting }) => ({
    loading: info.status,
    currentUser: user.currentUser || {},
    ...setting,
  }))(InfoCheck),
);
